package com.example.datn.manager;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import android.media.MediaPlayer;
import android.util.Log;

import com.example.datn.objects.EmptySong;
import com.example.datn.objects.SongObject;


public class SongManager {
	
	public static final SongManager INSTANCE = new SongManager();
	ArrayList<SongObject> ListSong = new ArrayList<SongObject>();
	private SongObject currentSong = null;
	private MediaPlayer currentMediaPlayer = null;
	
	public SongManager() {}
	
	public ArrayList<SongObject> getListSong() {
		return ListSong;
	}

	public void setListSong(ArrayList<SongObject> listSong) {
		this.ListSong = listSong;
	}
	
	public SongObject getSong(int id){
		ArrayList<SongObject> listSong = SongManager.getInstance().getListSong();
		
		int length = listSong.size();
		
		for(int i = 0; i < length; i++)
			if(listSong.get(i).songNo == id)
				return listSong.get(i);
		
		return new EmptySong();
	}
	
	public static String convertTime(int sec){
		String time = TimeUnit.SECONDS.toMinutes(sec) + ":"
				+ (TimeUnit.SECONDS.toSeconds(sec) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(sec)));
//		String time = "0" + sec/60 +":";// + sec%60;
//		if(sec%60 < 10)
//			time += "0"+ sec%60;
//		else
//			time += sec%60;
		return time; 
	}
	
	public static void killMediaPlayer(MediaPlayer mediaPlayer) {
		try {
			mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }
	}
	
	public static SongObject getNextSong(){
		int position = SongManager.getInstance().getCurrentSong().songNo-1;		
		//kiem tra xem bai hat hien tai da la bai hat cuoi chua
		if(position == SongManager.getInstance().getListSong().size()-1)
			position = 0;
		else
			position += 1;
		
		SongObject song = SongManager.getInstance().getListSong().get(position);		
		return song;
	}
	
	public static SongObject getPreviousSong(){
		int position = SongManager.getInstance().getCurrentSong().songNo-1;		
		//kiem tra xem bai hat hien tai co' la bai hat dau khong
		if(position == 0)
			position = SongManager.getInstance().getListSong().size()-1;
		else
			position -= 1;
		SongObject song = SongManager.getInstance().getListSong().get(position);		
		return song;
	}
	
	public static SongManager getInstance(){
		return INSTANCE;
	}

	public SongObject getCurrentSong() {
		return currentSong;
	}

	public void setCurrentSong(SongObject currentSong) {
		this.currentSong = currentSong;
	}

	public MediaPlayer getCurrentMediaPlayer() {
		return currentMediaPlayer;
	}

	public void setCurrentMediaPlayer(MediaPlayer currentMediaPlayer) {
		this.currentMediaPlayer = currentMediaPlayer;
	} 
	
	public void release(){
		this.ListSong.clear();
		this.currentMediaPlayer = null;
		this.currentSong = null;
	}
}

